<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Touch
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: NotificationsController.php 2011-04-26 11:18:13 mirlan $
 * @author     Mirlan
 */

/**
 * @category   Application_Extensions
 * @package    Touch
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Activity_NotificationsController extends Core_Controller_Action_Standard {
	
	public function init() {
		$this->_helper->requireUser ();
	}
	
	public function indexAction() {
		// Get navigation
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'touch' )->getNavigation ( 'activity_main' );
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->notifications = $notifications = Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' )->getNotificationsPaginator ( $viewer );
		$notifications->setItemCountPerPage ( 5 );
		$notifications->setCurrentPageNumber ( $this->_getParam ( 'page', 2 ) );
	}
	
	public function requestsAction() {
		// Get navigation
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'touch' )->getNavigation ( 'activity_main' );
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->requests = $requests = Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' )->getRequestsPaginator ( $viewer );
		$requests->setItemCountPerPage ( 2 );
		$requests->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
	}
	
	public function markreadAction() {
		$request = Zend_Controller_Front::getInstance ()->getRequest ();
		
		$action_id = $request->getParam ( 'actionid', 0 );
		
		$notificationsTable = Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' );
		$db = $notificationsTable->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$notification = Engine_Api::_ ()->getItem ( 'activity_notification', $action_id );
			$notification->read = 1;
			$notification->save ();
			// Commit
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		return;
	}
	
	public function updateAction() {
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		if ($viewer->getIdentity ()) {
			$this->view->notificationCount = $notificationCount = ( int ) Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' )->hasNotifications ( $viewer );
		}
		
		$request = Zend_Controller_Front::getInstance ()->getRequest ();
		$this->view->notificationOnly = $request->getParam ( 'notificationOnly', false );
		
		$this->view->text = $notificationCount;
	}
}